package com.wallace.downloadserver.wrapper

import java.io.ByteArrayOutputStream
import java.io.IOException
import javax.servlet.ServletOutputStream
import javax.servlet.WriteListener
import javax.servlet.http.HttpServletResponse
import javax.servlet.http.HttpServletResponseWrapper


/**
 * @Description:
 * TODO
 * @author Wallace
 * @Date 2022/1/21 16:18
 */
class EncryptResponseWrapper(httpServletResponse: HttpServletResponse?) : HttpServletResponseWrapper(httpServletResponse) {
    private val buffer: ByteArrayOutputStream = ByteArrayOutputStream()
    private val out: ServletOutputStream

    init {
        out = WrapperOutputStream(buffer)
    }

    @Throws(IOException::class)
    override fun getOutputStream(): ServletOutputStream {
        return out
    }

    @Throws(IOException::class)
    override fun flushBuffer() {
        out.flush()
    }

    @Throws(IOException::class)
    fun getContent(): ByteArray {
        flushBuffer()
        return buffer.toByteArray()
    }

    internal inner class WrapperOutputStream(private val bos: ByteArrayOutputStream) : ServletOutputStream() {

        @Throws(IOException::class)
        override fun write(b: Int) {
            bos.write(b)
        }

        override fun isReady(): Boolean {
            return false
        }

        override fun setWriteListener(listener: WriteListener?) {

        }

    }
}